﻿using System;

namespace ArrayTest
{
    /// <summary>
    /// 该程序模拟16位单色图像的差分编码
    /// 程序会创建一个二维数组，表示图像中的每个像元，像元值在0-65535之间
    /// 使用“调试——内存监视”监测编码前后的数组内存占用
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            int[,] arr = {
            {41567, 61234, 54981, 52340, 41998, 55432, 49876, 59012, 62345, 43000},
            {63345, 45567, 59999, 50123, 54321, 46234, 40567, 49568, 63456, 53500},
            {59876, 54876, 43210, 65535, 60000, 49999, 51234, 44000, 48567, 55321},
            {61234, 51324, 53000, 59998, 45789, 50001, 40567, 54789, 61876, 44987},
            {59012, 47000, 56123, 50345, 54432, 52000, 41568, 46678, 54876, 60543},
            {64000, 51111, 43333, 52123, 59678, 48876, 60234, 43000, 46345, 61000},
            {57000, 46432, 59876, 54999, 50321, 60000, 43876, 53567, 62345, 49000},
            {53333, 55876, 43789, 55432, 51567, 62543, 60000, 44000, 48000, 51123},
            {50234, 52432, 47543, 61000, 49678, 53000, 59876, 44987, 41567, 54321},
            {48567, 50234, 57234, 53000, 49789, 62345, 54876, 41567, 52543, 53123}
            };
            Console.WriteLine("原始数组：");
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Console.Write("{0} ", arr[i, j]);
                }
                Console.Write("\n");
            }

            int[,] encodeArray = EncodeArray(arr);
            int arrayAvg = CalculateAvg(arr);
            int[,] decodeArray = DecodeArray(encodeArray,arrayAvg);
            Console.WriteLine("编码后数组：");
            for (int i = 0; i < encodeArray.GetLength(0); i++)
            {
                for (int j = 0; j < encodeArray.GetLength(1); j++)
                {
                    Console.Write("{0} ",encodeArray[i,j]);
                }
                Console.Write("\n");
            }
            Console.WriteLine("解码后数组：");
            for (int i = 0; i < decodeArray.GetLength(0); i++)
            {
                for (int j = 0; j < decodeArray.GetLength(1); j++)
                {
                    Console.Write("{0} ", decodeArray[i, j]);
                }
                Console.Write("\n");
            }

        }

        static int[,] EncodeArray(int[,] originArray)
        {
            int arrayAverage = CalculateAvg(originArray);
            int[,] encodeArray=new int[originArray.GetLength(0), originArray.GetLength(1)] ;
            for (int i = 0; i < originArray.GetLength(0); i++)
            {
                for (int j = 0; j < originArray.GetLength(1); j++)
                {
                    encodeArray[i, j] = originArray[i, j] - arrayAverage;
                }
            }
            return encodeArray;
        }


        static int[,] DecodeArray(int[,] originArray, int arrayAverage)
        {
            int[,] decodeArray = new int[originArray.GetLength(0), originArray.GetLength(1)];
            for (int i = 0; i < originArray.GetLength(0); i++)
            {
                for (int j = 0; j < originArray.GetLength(1); j++)
                {
                    decodeArray[i, j] = originArray[i, j] + arrayAverage;
                }
            }
            return decodeArray;
        }


        static int CalculateAvg(int[,] array)
        {
            int average;
            int sum = 0;
            if (array.Length == 0)
            {
                throw new Exception("数组为null");
            }
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    sum += array[i, j];
                }
            }
            return average = sum / array.Length;
        }
    }
}
